package com.voxel.sense.infomsg.module.user.search;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.EventLog;

import com.example.com.common.utils.ListUtil;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.module.common.VRecyclerFrag;
import com.voxel.sense.infomsg.module.user.audit.AuditUserActivity;
import com.voxel.sense.infomsg.module.user.staff.StaffAdapter;
import com.voxel.sense.infomsg.module.user.staff.StaffListActivity;
import com.voxel.sense.infomsg.module.user.staff.details.StaffDetailsActivity;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import cyy.example.com.facotry.constants.Constants;
import cyy.example.com.facotry.constants.EventBusConstants;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.EventEntity;
import cyy.example.com.facotry.model.api.BaseEntity;
import cyy.example.com.facotry.model.api.response.StaffEntity;

/**
 * Created by ChenYiYao on 2018/8/6.
 */
public class SearchUserFrag extends VRecyclerFrag<SearchUserContract.Presenter, BaseEntity>
        implements SearchUserContract.View<BaseEntity> {

    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;

    private StaffAdapter mAdapter;
    private String mSearchConditon;
    private String mStatusCondition;

    @Override
    protected int getContentLayoutId() {
        return R.layout.layout_recycler_emptty;
    }



    @Override
    protected SearchUserContract.Presenter initPresenter() {
        Bundle arguments = getArguments();
        mStatusCondition = arguments.getString(IntentConstants.STRING_TYPE, Constants.FORMAL_USER_TYPE);
        return new SearchPresenter(this, mStatusCondition);
    }


    @Override
    protected void initData() {
        super.initData();
        refresh_layout.setEnableRefresh(false);
        //内容不满一页时，不能加载更多
        mAdapter = new StaffAdapter(new RecyclerAdapter.AdapterListenerImpl<StaffEntity.SysUserListBean>() {
            @Override
            public void onItemClick(RecyclerAdapter.ViewHolder holder, StaffEntity.SysUserListBean entity) {
                super.onItemClick(holder, entity);
                if (Constants.INFOMARL_USER_YPE.equals(mStatusCondition)) {
                    AuditUserActivity.show(getContext(),entity);
                } else {
                    StaffDetailsActivity.show(getContext(), entity, entity.getUserId());
                }
            }
        });
        recycle_view.setLayoutManager(new LinearLayoutManager(getContext()));
        LinearItemDecoration linearItemDecoration = new LinearItemDecoration(R.drawable.recycle_divider_10dp);
        linearItemDecoration.setDrawFirstItem(true).setDrawVerticalModelLeftAndRightBorder(true,true);
        recycle_view.addItemDecoration(linearItemDecoration);
        recycle_view.setAdapter(mAdapter);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        refresh_layout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                mPresenter.doSearch(mSearchConditon);
                refresh_layout.setNoMoreData(false);
            }

            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                if (NetworkUtils.isConnected()) {
                    mPresenter.doLoadMoreData();
                } else {
                    showError(getString(R.string.network_unavailable));
                    refresh_layout.finishLoadMore();
                }
            }
        });
    }


    @Override
    public RecyclerAdapter getRecyclerViewAdapter() {
        return mAdapter;
    }


    @Override
    public void onRefreshFinish() {
        super.onRefreshFinish();
        refresh_layout.setEnableRefresh(false);
    }

    @Override
    public void onUiRefresh(String conditon) {
        this.mSearchConditon = conditon;
        //拉起刷新ui事件
        empty_view.triggerOk();
        refresh_layout.setEnableRefresh(true);
        refresh_layout.autoRefresh();
    }

}
